package com.internetCafes.spms.web.extend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.internetCafes.spms.common.base.BaseWebController;
import com.internetCafes.spms.core.annotation.SysLog;
import com.internetCafes.spms.core.utils.DateUtils;
import com.internetCafes.spms.core.utils.R;
import com.internetCafes.spms.web.extend.model.EntCertiIso;
import com.internetCafes.spms.web.extend.model.EntCertiIsoRenewal;
import com.internetCafes.spms.web.extend.model.EntCertiQuali;
import com.internetCafes.spms.web.extend.service.IEntCertiIsoRenewalService;
import com.internetCafes.spms.web.extend.service.IEntCertiIsoService;
import com.internetCafes.spms.web.extend.vo.EntCertiIsoRenewalVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LinYe
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/cert/entIsoRenewal")
public class EntCertiIsoRenewalController extends BaseWebController {

    @Autowired
    IEntCertiIsoRenewalService entCertiIsoRenewalService;

    @Autowired
    IEntCertiIsoService entCertiIsoService;

    @SysLog("保存ISO证书流转信息")
    @PostMapping("/save")
    @RequiresPermissions("certificate:certificateInfo:save")
    public R save(@RequestBody EntCertiIsoRenewal entity) {
        entCertiIsoRenewalService.save(entity);
        EntCertiIso entCertiIso = new EntCertiIso();
        entCertiIso.setId(entity.getCertificateId());
        entCertiIso.setExpireDate(entity.getNewExpireDate());
        entCertiIsoService.updateById(entCertiIso);
        return R.ok();
    }

    @SysLog("作废ISO证书流转信息")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("certificate:certificateInfo:delete")
    public R delete(@PathVariable("id") Long id) {
        if (entCertiIsoRenewalService.removeById(id)) {
            return R.ok();
        } else {
            return R.error("您要删除的证书不存在");
        }
    }

    @SysLog("更新ISO证书流转信息")
    @PostMapping("/update")
    @RequiresPermissions("certificate:certificateInfo:update")
    public R update(@RequestBody EntCertiIsoRenewal entity) {
        if (entCertiIsoRenewalService.updateById(entity)) {
            entCertiIsoRenewalService.save(entity);
            EntCertiIso entCertiIso = new EntCertiIso();
            entCertiIso.setId(entity.getCertificateId());
            entCertiIso.setExpireDate(entity.getNewExpireDate());
            entCertiIsoService.updateById(entCertiIso);
            return R.ok();
        } else {
            return R.error("您要更新的证书不存在");
        }
    }

    @GetMapping("/getDetail/{id}")
    @RequiresPermissions("certificate:certificateInfo:info")
    public R get(@PathVariable("id") Long id) {
        EntCertiIsoRenewal entCertiIsoRenewal = entCertiIsoRenewalService.getById(id);
        if (entCertiIsoRenewal != null) {
            return R.ok().put("data", entCertiIsoRenewal);
        } else {
            return R.error("您要查询的证书不存在");
        }
    }

    @PostMapping("/paging")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R paging(@RequestBody EntCertiIsoRenewalVo entCertiIsoRenewalVo) {
        Page<EntCertiIsoRenewal> page=new Page<>(entCertiIsoRenewalVo.getPageNum(),entCertiIsoRenewalVo.getPageSize());
        QueryWrapper<EntCertiIsoRenewal> entCertiIsoRenewalQueryWrapper = new QueryWrapper<>(entCertiIsoRenewalVo);
        if (entCertiIsoRenewalVo.getOrderBy()!=null) {
            entCertiIsoRenewalQueryWrapper.orderBy(entCertiIsoRenewalVo.getOrderBy() != null, entCertiIsoRenewalVo.isAscFlag(), com.internetCafes.spms.common.utils.StringUtils.HumpToUnderline(entCertiIsoRenewalVo.getOrderBy()));
        }
        entCertiIsoRenewalQueryWrapper.orderBy(true,false,"id");
        if (entCertiIsoRenewalVo.getValidityType()!=null) {
            switch (entCertiIsoRenewalVo.getValidityType()) {
                case -1:
                    entCertiIsoRenewalQueryWrapper.lt("new_expire_date", new Date());
                    break;
                case 0:
                    entCertiIsoRenewalQueryWrapper.gt("new_expire_date", new Date());
                    entCertiIsoRenewalQueryWrapper.lt("new_expire_date", entCertiIsoRenewalVo.getDays() != null &&  entCertiIsoRenewalVo.getDays() != "" ? DateUtils.getBeAfDay(Integer.parseInt(entCertiIsoRenewalVo.getDays())) : entCertiIsoRenewalVo.getMonths() != null && entCertiIsoRenewalVo.getMonths() != "" ? DateUtils.getBeAfMonth(Integer.parseInt(entCertiIsoRenewalVo.getMonths())) : null);
                    break;
                case 2:
                    entCertiIsoRenewalQueryWrapper.lt("new_expire_date", new Date());
                    entCertiIsoRenewalQueryWrapper.or(wrapper -> wrapper.gt("new_expire_date", new Date()).lt("new_expire_date", entCertiIsoRenewalVo.getDays() != null ? DateUtils.getBeAfDay(Integer.parseInt(entCertiIsoRenewalVo.getDays())) : entCertiIsoRenewalVo.getMonths() != null ? DateUtils.getBeAfMonth(Integer.parseInt(entCertiIsoRenewalVo.getMonths())) : null));
            }
        }
        IPage<EntCertiIsoRenewal> entCertiIsoRenewalIPage = entCertiIsoRenewalService.page(page,entCertiIsoRenewalQueryWrapper);
        return R.ok().put("data", entCertiIsoRenewalIPage);
    }

    @PostMapping("/list")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R list(@RequestBody EntCertiIsoRenewalVo entCertiIsoRenewalVo) {
        QueryWrapper<EntCertiIsoRenewal> entCertiIsoRenewalQueryWrapper = new QueryWrapper<EntCertiIsoRenewal>(entCertiIsoRenewalVo);
        entCertiIsoRenewalQueryWrapper.orderBy(entCertiIsoRenewalVo.getOrderBy()!=null,entCertiIsoRenewalVo.isAscFlag(),entCertiIsoRenewalVo.getOrderBy());
        List<EntCertiIsoRenewal> list = entCertiIsoRenewalService.list(entCertiIsoRenewalQueryWrapper);
        return R.ok().put("data", list);
    }

    @PostMapping("/count")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R count(@ModelAttribute EntCertiIsoRenewal entCertiIsoRenewal) {
        QueryWrapper<EntCertiIsoRenewal> entCertiIsoRenewalQueryWrapper = new QueryWrapper<EntCertiIsoRenewal>(entCertiIsoRenewal);
        int count = entCertiIsoRenewalService.count(entCertiIsoRenewalQueryWrapper);
        return R.ok().put("count", count);
    }
}
